package cn.gov.msa.ws;

import java.util.List;

import org.springframework.context.ApplicationContext;

import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Question;
import cn.gov.msa.model.Questions;
import cn.gov.msa.util.GsonHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**   
* @Title: QuestionService.java 
* @Package cn.gov.msa.ws 
* @Description: 问卷调查(用一句话描述该文件做什么) 
* @author 冷云鹏  
* @date 2018年4月2日 上午10:08:56 
*/
public class QuestionsService {
	
	public static String setData(ApplicationContext ctx, String params) throws Exception {
				
	    JSONObject jsonResult = new JSONObject();
	    cn.gov.msa.service.QuestionsService questionsService = (cn.gov.msa.service.QuestionsService) ctx.getBean("QuestionsService");  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		int pageNum = 1;
		int pageSize = 10;
		if(jsonParams.has("pageNum"))
		{
			pageNum = jsonParams.getInt("pageNum");
			jsonParams.remove("pageNum");
		}
		if(jsonParams.has("pageSize"))
		{
			pageSize = jsonParams.getInt("pageSize");
			jsonParams.remove("pageSize");
		}	
		Questions questions = GsonHelper.convertEntity(jsonParams.toString(), Questions.class);
		List<Questions> questionsList = questionsService.selectData(pageNum, pageSize, questions);
	    JSONArray jaResult = JSONArray.fromObject(questionsList);
	    //获取分页信息
        PageInfo<Questions> page = new PageInfo<Questions>(questionsList);
        
        jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", jaResult);	
		
		return jsonResult.toString();
	}
	
	
}
